<?php
namespace App\Http\Controllers\Api;
use App\V3\V3_1\Models\SupplierCustomerModel;
use Illuminate\Http\Request;
use Validator;

/**
 * 供应商客户
 * Class SupplierCustomerController
 * @package App\Http\Controllers\Api
 */
class SupplierCustomerController extends BaseController
{
    /**
     * 列表
     * change 在 2021/10/14 15:34 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function index(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'pageSize' =>'required|integer',//页面大小
            'currentPage' =>'required|integer',//当前页
            'name' =>'string',//搜索 客户名
//            'type' =>'required|integer'//类型：0-供应商 1-客户 2-全部
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        //查询条件
        $conditon[]=['a.main_id','=',$this->getMainUserId()];
//        if($data['type']!=2){
//            $conditon[]=['a.type','=',$data['type']];
//        }
        if(isset($data['name']) && !empty($data['name'])){
            $conditon[] = ['a.name','like','%'.$data['name'].'%'];
        }
        //执行sql
        $model = SupplierCustomerModel::select('a.id','a.compony_name','a.name','a.phone','a.address','a.created_at','b.username',)
            ->from('supplier_customer as a')
            ->leftJoin('admin as b', 'a.user_id', '=', 'b.id')
            ->where($conditon)
            ->orderBy('a.id','desc')
            ->paginate($data['pageSize'],['*'], '',$data['currentPage']);
        //响应
        return $this->success($model);
    }

    public function find(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//供应商客户表主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        //操作日志
        $model = SupplierCustomerModel::find($data['id']);
        return $this->success($model);
    }

    /**
     * 添加
     * change 在 2021/10/14 15:34 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function add(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'compony_name' =>'string',//公司名称
            'name' =>'required|string',//客户\供应商名称
//            'phone' =>'string',//联系方式
            'address' =>'string',//地址

        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        $data['type'] = !isset($data['type'])?0:$data['type'];
        $data['main_id'] = $this->getMainUserId();
        $data['user_id'] = $this->getUserId();
        $data['created_at'] = time();
        $newId = SupplierCustomerModel::insertGetId($data);
        //操作日志
        $this->log('添加供应商或客户',$data);
        return $this->success($newId);
    }

    /**
     * 编辑
     * change 在 2021/10/14 15:34 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function edit(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//供应商客户表主键id
//            'compony_name' =>'required|string',//公司名称
//            'name' =>'required|string',//客户\供应商名称
//            'phone' =>'required|string',//联系方式
//            'address' =>'required|string',//地址
//            'type' =>'required|integer',//类型：0-供应商 1-客户

        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }

        unset($data['type']);
        SupplierCustomerModel::where('id',$data['id'])->update($data);
        //操作日志
//        $type = SupplierCustomerModel::where('id',$data['id'])->value('type');
//        if($type==0){
//            $this->log('编辑供应商',$data);
//        }else{
//            $this->log('编辑客户',$data);
//        }
        return $this->success();
    }

    /**
     * 删除
     * change 在 2021/10/14 15:34 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function del(Request $request){
        //参数验证
        $validator = Validator::make($data = $request->except('role_id'), [
            'id' =>'required|integer',//供应商客户表主键id
        ]);
        //参数错误响应
        if ($info = $validator->errors()->first()) {
            return response()->json([
                'code' => 1,
                'msg' => $info,
                'data' => '',
            ]);
        }
        //操作日志
        $model = SupplierCustomerModel::find($data['id']);

        if(!empty($model)) {
            $model->delete();
        }

        if($model['type']==0){
            $this->log('删除供应商',$model);
        }else{
            $this->log('删除客户',$model);
        }
        $model->delete();
        return $this->success();
    }

    /**
     * 客户供应商下拉框
     * change 在 2021/10/14 15:36 创建
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function select(Request $request){
        $model = SupplierCustomerModel::select('id','name')
            ->where(['main_id'=>$this->getMainUserId()])
            ->get();
        return $this->success($model);
    }
}
